package com.sywb.chuangyebao.contract;

import android.content.Context;
import android.widget.ImageView;

import com.sywb.chuangyebao.Constants;
import com.sywb.chuangyebao.R;
import com.sywb.chuangyebao.bean.Page;
import com.sywb.chuangyebao.bean.User;
import com.sywb.chuangyebao.utils.GlideUtils;
import com.sywb.chuangyebao.utils.HttpCallback;
import com.sywb.chuangyebao.utils.HttpUtils;
import com.sywb.chuangyebao.utils.Utils;
import com.sywb.chuangyebao.view.AskActivity;

import org.bining.footstone.adapter.BaseRecyclerAdapter;
import org.bining.footstone.adapter.ViewHolderHelper;
import org.bining.footstone.adapter.listener.OnItemChildClickListener;
import org.bining.footstone.adapter.listener.OnItemClickListener;

import java.util.ArrayList;

public interface TutorContract {

    interface View extends BaseRefreshContract.View {

    }

    class Presenter extends BaseRefreshContract.Presenter<View> {

        private TutorAdapter recyclerAdapter;
        private int pageNo = 1;

        @Override
        public void onStart() {
            super.onStart();
            recyclerAdapter = new TutorAdapter(mContext);
            setRecyclerAdapter(recyclerAdapter);

            initData();
        }

        @Override
        public void initData() {
            super.initData();
            pageNo = 1;
            listUserTutor();
        }

        @Override
        public void onStartRefresh() {
            pageNo = 1;
            listUserTutor();
        }

        @Override
        public void onStartLoadMore() {
            pageNo++;
            listUserTutor();
        }

        @Override
        public void onRecyclerItemClick(int postion) {
            Utils.goUserIndex(mView, recyclerAdapter.getItem(postion).user_id);
        }

        public void listUserTutor() {
//            List<UserInfo> menuItems = new ArrayList<>();
//            for (int i = 0; i < 20; i++) {
//                menuItems.add(new UserInfo());
//            }
//            recyclerAdapter.setDatas(menuItems);
//            isNoData();

            HttpUtils.listUserTutor(0, Constants.PAGESIZE, pageNo, false, Constants.USER_TYPE_TUTOR, 0, "", "", new HttpCallback<Page<User>>() {
                @Override
                public void onSuccess(Page<User> userPage) {
                    refreshPage(userPage);
                }

                @Override
                public void onError(String strMsg) {
                    super.onError(strMsg);
                    showMessage(strMsg);
                    refreshPage(null);
                }

                @Override
                public void onFinish() {
                    super.onFinish();
                    onFinishAsync();
                    isNoData();
                }
            });
        }

        private void refreshPage(Page<User> pageInfo) {
            if (pageInfo != null && pageInfo.list != null) {
                if (pageNo == 1) {
                    pullToRefreshView.refreshFinish(true);
                    recyclerAdapter.clearDatas();
                }
                recyclerAdapter.notifyDataChangedAfterLoadMore(pageInfo.list);
                // recyclerAdapter.setLoadMoreEnable(pageInfo.page < pageInfo.lastPageNo);
            } else if (pageNo == 1) {
                pullToRefreshView.refreshFinish(false);
            } else {
                recyclerAdapter.notifyDataChangedAfterLoadMore(new ArrayList<User>());
            }
        }

        @Override
        public boolean useRecyclerDivider() {
            return false;
        }

        class TutorAdapter extends BaseRecyclerAdapter<User> implements OnItemChildClickListener {

            public TutorAdapter(Context context) {
                super(context, R.layout.item_tutor);
            }

            @Override
            protected int getDefItemViewType(int position) {
                if (position >= getDataCount()) {
                    return 0x00000333;
                }
                return super.getDefItemViewType(position);
            }

            @Override
            protected void setItemData(ViewHolderHelper viewHolderHelper, int i, User model) {
                viewHolderHelper.setOnItemChildClickListener(this);
                viewHolderHelper.setItemChildClickListener(R.id.tv_tutor_consult);

                ImageView imageView = viewHolderHelper.getView(R.id.iv_tutor_face);
                GlideUtils.getCircleImageToUrl(mActivity, model.avatar, imageView, R.drawable.head_image);

                viewHolderHelper.setText(R.id.tv_tutor_name, model.display_name);

                viewHolderHelper.setText(R.id.tv_tutor_goodat, "擅长领域：" + model.getGoodAtString());

                viewHolderHelper.setText(R.id.tv_tutor_other, model.fans_num + "粉丝  ·  " + model.answer_num + "回答");

            }

            @Override
            public void onItemChildClick(android.view.View view, int i) {
                switch (view.getId()) {
                    case R.id.tv_tutor_consult:
                        if (mView != null && Utils.checkIsNext(mView.getMyFragmentManager())) {
                            User user = mDatas.get(i);
                            mView.advance(AskActivity.class, AskActivity.COMING_TYPE_ASK, "", user.display_name, user.user_id);
                        }
                        break;
                }
            }
        }
    }


}

